package edu.dqsy.mzq_translate;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import edu.dqsy.mzq_translate.helper.ContactOpenHelper;

public class denglu extends AppCompatActivity {
    public static String user;
    private Button denglu;
    private EditText username;
    private EditText password;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_denglu);

        final ContactOpenHelper helper = new ContactOpenHelper(denglu.this,"userinfo.db",null,1);
        helper.getWritableDatabase();
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        denglu = findViewById(R.id.denglu);


        denglu.setOnClickListener(view -> {
            AlertDialog.Builder builder  = new AlertDialog.Builder(denglu.this);
            System.out.println("aaa");
            SQLiteDatabase db = helper.getWritableDatabase();
            String us = username.getText().toString();
            String pa = password.getText().toString();
            String s = "";
            System.out.println("("+us+")");
            Cursor c = db.rawQuery("SELECT password FROM userInfo where username=?",new String[]{us});
            if(c.getCount()!=0 && !us.equals(s)){
                while(c.moveToNext()){
                    String password = c.getString(0);
                    if (password.equals(pa) && !pa.equals(s)){
                        user = us;
                        Intent ii = new Intent(denglu.this, MainActivity.class);
                        startActivity(ii);
                    }else{
                        builder.setTitle("登录失败" ) ;
                        builder.setMessage("用户名或密码错误" ) ;
                        builder.setPositiveButton("是" ,  null );
                        builder.show();
                    }

                }
            }else{
                System.out.println("登录失败");

                builder.setTitle("登录失败" ) ;
                builder.setMessage("此用户未注册" ) ;
                builder.setPositiveButton("是" ,  null );
                builder.show();
            }
        });
    }
    public void gotozhuce(View v){
        Intent i = new Intent(denglu.this, zhuce.class);
        startActivity(i);
    }
}